$(function(){
	
	//get object with colros from plugin and store it.
	var objColors = $('body').data('sprFlat').getColors();
	var colours = {
		white: objColors.white,
		dark: objColors.dark,
		red : objColors.red,
		blue: objColors.blue,
		green : objColors.green,
		yellow: objColors.yellow,
		brown: objColors.brown,
		orange : objColors.orange,
		purple : objColors.purple,
		pink : objColors.pink,
		lime : objColors.lime,
		magenta: objColors.magenta,
		teal: objColors.teal,
		textcolor: '#5a5e63',
		gray: objColors.gray
	}
	
	//------------- Weather icons -------------//
	$.ajax({
		type:"post",
		url:"weather",
		success:function(res){
			if(res.status==1000){
				var weather=res.data;
				weatherload(weather,colours);
			}else{
				return;
			}
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			setTimeout(function(){
				location.reload();
			},2000);
		}
	});
})

function weatherload(data,colours){
	$("#city").text(data.city);
	$("#temp0").html(data.wendu+"&deg;C");
	
	var today = new Skycons({
		"color": colours.white,
		"resizeClear": true
	});
	var forecastWeather = new Skycons({
		"color": colours.dark,
		"resizeClear": true
	});
	
	var forecast=data.forecast;
	for (var i = 0; i < forecast.length; i++) {

		var date="date"+i;
		var temp="temp"+i;
		var forecastStr="forecast"+i;
		var type="";
		
		$("#"+date).text(forecast[i].date);
		switch (forecast[i].type) {
		case "多云":
			type="partly-cloudy-day"
			break;
		case "小雨":
			type="rain";
			break;
		case "雷阵雨":
			type="sleet";
			break;
		case "阴":
			type="cloudy";
			break;
		case "晴":
			type="clear-day";
			break;
		case "中雨":
			type="sleet";
			break;
		default:
			break;
		}
		
		if(i==0){
			today.set("weather-now", type);
			today.play();
		}else{
			$("#"+temp).text(forecast[i].low+"~"+forecast[i].high);
			//set forecast icons too
			forecastWeather.set(forecastStr, type);
			forecastWeather.play();
		}		
	
	}
}